package sv.gob.mh.dgii.ssc.controller.configuration;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.springframework.security.core.context.SecurityContextHolder;
import sv.gob.mh.dgii.security.SscUserDetailsImpl;

public class MainController {

    protected final static String URLPARAMS_TABLEID_STRING = "tableId";
    protected final static String URLPARAMS_PAGE_STRING = "page";
    protected final static String URLPARAMS_ORDERBY_STRING = "order-by";
    protected final static String URLPARAMS_SWITCHORDER_STRING = "switchOrder";
    protected BasicViewForm form;

//    @ExceptionHandler(Exception.class)
//    @ResponseBody
//    public String handleException1(Exception ex) {
//        return ex.getMessage();
//    }

    public SscUserDetailsImpl getPrincipal() {
        return (SscUserDetailsImpl) SecurityContextHolder.getContext().getAuthentication().getPrincipal();        
    }

    public void setOrderByPaginator(HttpServletRequest req) {
        if (req.getSession().getAttribute("direction") == null) {
            req.getSession().setAttribute("direction", "asc");
        } else if (req.getSession().getAttribute("direction").equals("asc")) {
            req.getSession().setAttribute("direction", "desc");
        } else if (req.getSession().getAttribute("direction").equals("desc")) {
            req.getSession().setAttribute("direction", "asc");
        }
    }

    public String getUsuario() {
        String user = "USER";
        try {
            SscUserDetailsImpl principals = getPrincipal();
            user = principals.getUsuario();
        } catch (Exception e) {
        }
        return user;
    }

    public String getUnidad() {
        String user = null;
        try {
            SscUserDetailsImpl principals = getPrincipal();
            //user = principals.getUnidad();
        } catch (Exception e) {
        }
        return user;
    }
    
     public String getAdmTrib() {
        String user = null;
        try {
            SscUserDetailsImpl principals = getPrincipal();
            user = principals.getCadmTrib();
        } catch (Exception e) {
        }
        return user;
    }

    public Object getSessionValue(HttpServletRequest req, String key) {
        return req.getSession().getAttribute(key);
    }

    public void setSessionValue(HttpServletRequest req, String key, Object obj) {
        req.getSession().setAttribute(key, obj);
    }

    public void removeSessionValue(HttpServletRequest req, String key) {
        req.getSession().removeAttribute(key);
    }

    public BasicViewForm getForm() {
        return form;
    }

    public void setForm(BasicViewForm form) {
        this.form = form;
    }

    public void addError(Log log, Exception e) {
        if (form != null) {
            form.addError(log, e);
        }
    }

    public void addError(Log log, String e) {
        if (form != null) {
            form.addError(log, e);
        }
    }

    public void clearErrores() {
        if (form != null) {
            form.clearErrores();
        }
    }

    public void clearErrors() {
        clearErrores();
    }

    public List<Exception> getErrores() {
        if (form != null) {
            return form.getErrores();
        }
        return null;
    }

    public void setErrores(List<Exception> errores) {
        if (form != null) {
            form.setErrores(errores);
        }
    }
}
